//
// Created by admin on 2019/4/11. 这
//
#include "debug.h"

#ifdef linux
void itoa(int i,char* string)
{
    int power,j;
    j=i;
    for(power=1;j>=10;j/=10)
        power*=10;
    for(;power>0;power/=10)
    {
        *string++='0'+i/power;
        i%=power;
    }
    *string='\0';
}
#endif

int PrintDumpObjvoid (void *dst,int rowNum,int num,bool ifAsii){
    char out [2048] = {0};
    int row = num / rowNum;
    int left = num %rowNum;
    if (left != 0){
        row += 1;
    }
    snprintf(out,5,"LN: ");
    for (int z = 1;z < rowNum + 1; z++) {
        char tmp[6] = {0};
        sprintf(tmp,"%02d ",z);
        strcat(out,tmp);
    }
    strcat(out," ");

    if (ifAsii) {
        for (int z = 1;z < rowNum + 1; z++) {
            char tmp[6] = {0};
            sprintf(tmp,"%02d ",z);
            strcat(out,tmp);
        }
    }
    strcat(out,"\r\n");
    for(int i = 0;i < row;i ++) {
        string rowstr;
        char tmp[6] = {0};
        sprintf(tmp,"%02x",i + 1);
        rowstr += string(tmp) + ": ";
        //last row
        if (i == row -1 && left != 0){
            for (int z = 0;z < rowNum; z++) {
                char tmp[6] = {0};
                if (z < left){
                    sprintf(tmp,"%02x",((unsigned char)(((unsigned char *)dst)[ z + i * rowNum])));
                }else{
                    sprintf(tmp,"  ");
                }
                rowstr += string(tmp) + " ";
                if (z == left - 1) {
                    if (!ifAsii)
                        rowstr += "\r\n";
                }
            }
            rowstr += " ";
            if(ifAsii) {
                for (int z = 0;z < left; z++) {
                    char tmp[6] = {0};
                    sprintf(tmp,"%c",((unsigned char)(((unsigned char *)dst)[ z + i * rowNum])));
                    rowstr += string(tmp) + " ";
                    if (z == left - 1){
                        rowstr += "\r\n";
                    }
                }
            }
        }else{
            for (int z = 0;z < rowNum; z++) {
                char tmp[6] = {0};
                sprintf(tmp,"%02x",((unsigned char)(((unsigned char *)dst)[ z + i * rowNum])));
                rowstr += string(tmp) + " ";
                if (!ifAsii){
                    if (z == rowNum - 1){
                        rowstr += "\r\n";
                    }
                }
            }
            if(ifAsii){
                rowstr += " ";
                for (int z = 0;z < rowNum; z++) {
                    char tmp[6] = {0};
                    sprintf(tmp,"%c",((unsigned char)(((unsigned char *)dst)[ z + i * rowNum])));
                    rowstr += string(tmp) + "   ";
                    if (z == rowNum - 1){
                        rowstr += "\r\n";
                    }
                }
            }
        }
        strcat(out,rowstr.c_str());
    }
    cout << out << "\r\n";
    return  0;
}

int dumpObj(void *dst,int rowNum,int num,bool ifAsii,char *out) {
    if (NULL == out) {
        return -1;
    }
    int row = num / rowNum;
    int left = num %rowNum;
    if (left != 0){
        row += 1;
    }
    sprintf(out,"LN: ");
    for (int z = 1;z < rowNum + 1; z++) {
        char tmp[4] = {0};
        sprintf(tmp,"%02d ",z);
        strcat(out,tmp);
    }
    strcat(out," ");

    if (ifAsii) {
        for (int z = 1;z < rowNum + 1; z++) {
            char tmp[4] = {0};
            sprintf(tmp,"%02d ",z);
            strcat(out,tmp);
        }
    }
    strcat(out,"\r\n");
    for(int i = 0;i < row;i ++) {
        string rowstr;
        char tmp[4] = {0};
        sprintf(tmp,"%02x",i + 1);
        rowstr += string(tmp) + ": ";
        //last row
        if (i == row -1 && left != 0){
            for (int z = 0;z < rowNum; z++) {
                char tmp[4] = {0};
                if (z < left){
                    sprintf(tmp,"%02x",((unsigned char)(((unsigned char *)dst)[ z + i * rowNum])));
                }else{
                    sprintf(tmp,"  ");
                }
                rowstr += string(tmp) + " ";
                if (z == left - 1) {
                    if (!ifAsii)
                        rowstr += "\r\n";
                }
            }
            rowstr += " ";
            if(ifAsii) {
                for (int z = 0;z < left; z++) {
                    char tmp[4] = {0};
                    sprintf(tmp,"%c",((unsigned char)(((unsigned char *)dst)[ z + i * rowNum])));
                    rowstr += string(tmp) + " ";
                    if (z == left - 1){
                        rowstr += "\r\n";
                    }
                }
            }
        }else{
            for (int z = 0;z < rowNum; z++) {
                char tmp[3] = {0};
                sprintf(tmp,"%02x",((unsigned char)(((unsigned char *)dst)[ z + i * rowNum])));
                rowstr += string(tmp) + " ";
                if (!ifAsii){
                    if (z == rowNum - 1){
                        rowstr += "\r\n";
                    }
                }
            }
            rowstr += " ";
            if(ifAsii){
                for (int z = 0;z < rowNum; z++) {
                    char tmp[4] = {0};
                    sprintf(tmp,"%c",((unsigned char)(((unsigned char *)dst)[ z + i * rowNum])));
                    rowstr += string(tmp) + "   ";
                    if (z == rowNum - 1){
                        rowstr += "\r\n";
                    }
                }
            }
        }
        strcat(out,rowstr.c_str());
    }
    cout << out << "\r\n";
    return  0;
}


